<!-- 员工详情页面 -->
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form
          ref="employeeForm"
          :model="userInfo"
          :rules="rules"
          label-width="220px"
        >
          <!-- 姓名 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="nickName">
                <el-input v-model="userInfo.nickName" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 年龄 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="年龄" prop="age">
                <el-input v-model="userInfo.age" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <!-- 工号是系统生成的  禁用这个组件-->
                <el-input
                  v-model="userInfo.workNumber"
                  disabled
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机  :disabled="!!$route.params.userId" 手机号码在编辑模式下不可以修改-->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号" prop="phoneNum">
                <el-input
                  v-model="userInfo.phoneNum"
                  :disabled="!!$route.params.userId"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="deptId">
                <!-- 放置及联部门组件 会单独封装-->
                <!-- inputW样式会给到selectTree中 template第一层的组件 -->
                <select-tree v-model="userInfo.deptId" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="性别" prop="sex">
                <el-select v-model="userInfo.sex" class="inputW">
                  <el-option label="男" :value="true" />
                  <el-option label="女" :value="false" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="employmentTime">
                <el-date-picker
                  v-model="userInfo.employmentTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- 放置上传图片组件 -->
                <ImageUpload v-model="userInfo.avatar"></ImageUpload>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row  type="flex" justify="center" style="width: 620px;">
            <el-col :span="8"></el-col>
            <el-col :span="8">
              <el-button type="primary" @click="saveData">确定</el-button>
            </el-col>
            <el-col :span="8">
              <el-button @click="$router.push('/employee')">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>
  
  <script>
import SelectTree from "./components/select-tree.vue";
import ImageUpload from "./components/image-upload.vue";
import employeeApi from "@/api/employee";
export default {
  components: { SelectTree,ImageUpload },
  data() {
    return {
      userInfo: {
        nickName: "", // 用户名
        age:"",//年龄
        phoneNum: "", // 手机号
        workNumber: "", // 工号
        sex: null, // 性别
        deptId: null, // 部门id
        employmentTime: "", // 入职时间
        avatar: "", // 员工头像
      },
      rules: {
        nickName: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "姓名为1-10位",
          },
        ],
        age: [
          { required: true, message: "请输入年龄", trigger: "blur" },
        ],
        phoneNum: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            //   pattern 正则表达式
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        sex: [
          { required: true, message: "请选择性别", trigger: "blur" },
        ],
        deptId: [
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        employmentTime: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        // correctionTime: [
        //   { required: true, message: "请选择转正时间", trigger: "blur" },
        //   {
        //     validator: (rule, value, callback) => {
        //       if (this.employeeInfo.employmentTime) {
        //         if (new Date(this.employeeInfo.employmentTime) > new Date(value)) {
        //           callback(new Error("转正时间不能小于入职时间"));
        //           return;
        //         }
        //       }
        //       callback();
        //     },
        //   },
        // ],
      },
    };
  },
//   初始化后就需要根据路由参数中的deptId获取部门参数
  created() {
    // 如何获取路由参数的中id
    // console.log(this.$route.params.userId);
    //如何路由参数中有userId才会获取员工详情数据
    if(this.$route.params.userId){
        this.getEmployeeDetail();
    }
  },
  methods: {
    //根据userId获取员工数据
    async getEmployeeDetail() {
      //原本我们的this.userInfo中是没有userId这个属性和值的，但是我们在调用下面这个接口之后，返回的数据中有userId,它会重新给我们的this.userInfo添加上userId这个值
      this.userInfo = await employeeApi.getEmployeeDetail(this.$route.params.userId);
    },
    //员工按钮确定
    saveData() {
      this.$refs.employeeForm.validate(async (isOK) => {
        if (isOK) {
          // 编辑模式
          if (this.$route.params.userId) {
            // 编辑模式
            // 校验通过
            await employeeApi.updateEmployee(this.userInfo);
            this.$message.success("更新员工成功");
          } else {
            // 新增模式
            // 校验通过
            await employeeApi.addEmployee(this.userInfo);
            this.$message.success("新增员工成功");
          }
        //添加/修改完成之后跳转到员工列表页面
          this.$router.push("/employee");
        }
      });
    },
  },
};
</script>
  
<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
  
  
  
  